package com.dididoctor.doctor.Activity.Main.Inquiry.NewInquiry.AddSelectAnswer;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Utils.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： AddSelectAnswerAdapter
 * @包名称： com.dididoctor.doctor.Activity.Main.Inquiry.NewInquiry.AddSelectAnswer
 * @创建人： zhanjunjie
 * @创建时间：2016/4/22 15:29
 * @修改人：
 * @修改时间：2016/4/22 15:29
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class AddSelectAnswerAdapter extends BaseAdapter {


    private List<AnswerBean> answerBeans = new ArrayList<>();
    private Context context;

    public AddSelectAnswerAdapter(Context context, List<AnswerBean> datas) {
        this.answerBeans = datas;
        this.context = context;
    }

    @Override
    public int getCount() {
        return answerBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return answerBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_add_answer, null);
        AnswerHolder answerHolder = null;
        if (answerHolder == null) {
            answerHolder = new AnswerHolder();
            answerHolder.mEtopions = (EditText) view.findViewById(R.id.et_options);
            answerHolder.mRlDelete = (RelativeLayout) view.findViewById(R.id.rl_delete);
            view.setTag(answerHolder);
            convertView = view;
        } else {
            answerHolder = (AnswerHolder) convertView.getTag();
        }
        answerHolder.mEtopions.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                AnswerBean option = answerBeans.get(position);
                if (option == null) {
                    option = new AnswerBean();
                }
                option.setOpction(s.toString());
            }

        });
        //如果hashMap不为空，就设置的editText
        if (answerBeans.get(position) != null) {
            if (!Util.isEmpty(answerBeans.get(position).getOpction())) {
                answerHolder.mEtopions.setText(answerBeans.get(position).getOpction());
            } else {
                answerHolder.mEtopions.setText("");
            }

        }

        answerHolder.mRlDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (answerBeans.size() == 3) {
                    Toast.makeText(context, "至少有三个选项", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    answerBeans.remove(position);
                    notifyDataSetChanged();
                }

            }
        });
        return convertView;
    }

    class AnswerHolder {
        EditText mEtopions;
        RelativeLayout mRlDelete;
    }


}
